package com.ss.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.ss.MyTest;
import com.ss.domain.Student;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class GenerateDataTools {
    // 生成具有随机数据的excel文件
    public static void generateExcelData(){
        String fileName = MyTest.class.getResource("/").getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx";
        // 这里 指定文件
        try (ExcelWriter excelWriter = EasyExcel.write(fileName, Student.class).build()) {
            // 去调用写入,这里我调用了五次，实际使用时根据数据库分页的总的页数来。这里最终会写到5个sheet里面
            for (int i = 0; i < 20; i++) {
                // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样
                WriteSheet writeSheet = EasyExcel.writerSheet(i, "模板" + i).build();
                // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
                List<Student> list = new ArrayList<>();

                // 随机生成50000条数据
                for(int d = 1; d <= 50000; d++){
                    Student row = new Student();
                    row.setId(50000 * i +d);
                    Random random = new Random();
                    String randomTwoLetters = "" + (char) ('A' + random.nextInt(26)) + (char) ('A' + random.nextInt(26));
                    row.setName(randomTwoLetters);
                    int randomNumber = 10 + random.nextInt(41);
                    row.setAge(randomNumber);
                    list.add(row);
                }

                excelWriter.write(list, writeSheet);
            }
        }
    }
}
